# Copyright (c) 2025-2025 Huawei Technologies Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time

from ms_service_profiler import Profiler, Level
from .vllm_hooker_base import VLLMHookerBase


# generate -> add_request -> schedule -> execute_model
# 在请求进入引擎时记录时间戳，用于后续计算队列等待时间。
class EngineRequestTrackerHook(VLLMHookerBase):
    vllm_version = ("0.6.3", "0.6.3")

    def init(self):
        from vllm.engine.llm_engine import LLMEngine
        from vllm.engine.async_llm_engine import AsyncLLMEngine

        def add_request_maker(ori_func):
            def add_request(this, request_id, prompt, *args, **kwargs):
                # 记录请求进入系统的时间                
                profiler = Profiler(Level.INFO)
                profiler.domain("http").res(request_id).event("httpReq")
                return ori_func(this, request_id, prompt, *args, **kwargs)

            return add_request

        self.do_hook([LLMEngine.add_request, AsyncLLMEngine.add_request], add_request_maker)


# 采集请求返回的数据
class LLMEngineHook(VLLMHookerBase):
    vllm_version = ("0.6.3", "0.6.3")

    def init(self):
        from vllm.engine.llm_engine import LLMEngine

        def validate_output_maker(ori_func):
            def validate_output(output, output_type):
                profiler_recv = Profiler(Level.INFO)
                profiler_reply = Profiler(Level.INFO)
                if output.finished is True:
                    request_id = output.request_id
                    input_token_size = len(output.prompt_token_ids)
                    output_token_size = len(output.outputs[0].token_ids)
                    profiler_recv.domain("http").res(request_id).metric(
                        "recvTokenSize", input_token_size).event("httpRes")
                    profiler_reply.domain("http").res(request_id).metric(
                        "replyTokenSize", output_token_size).event("httpRes")
                return ori_func(output, output_type)

            return validate_output

        self.do_hook([LLMEngine.validate_output], validate_output_maker)


request_hookers = [EngineRequestTrackerHook, LLMEngineHook]
